package it.univpm.diiga.semanticsearch.ricerca;
import it.univpm.diiga.semanticsearch.servizi.Link;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;



public class SaveResultView extends JDialog {
	
	private JTextField path;
	private Ricerca ricerca;
	private JProgressBar progressBar;
	private JButton saveButton;


	/**
	 * Create the frame
	 */
	public SaveResultView(final List<Link> selectedResult) {
		super();
		setModal(true);
		setResizable(false);
	
		getContentPane().setLayout(null);
		setBounds(100, 100, 433, 190);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		progressBar = new JProgressBar(0,100);
		progressBar.setBounds(228, 121, 145, 20);
		progressBar.setVisible(false);
		getContentPane().add(progressBar);

		final JButton chooseButton = new JButton();
		chooseButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
					final JFileChooser fc = new JFileChooser();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
							   
			        int returnVal = fc.showOpenDialog(SaveResultView.this);

			        if (returnVal == JFileChooser.APPROVE_OPTION) {
			            File file = fc.getSelectedFile();
			            path.setText(file.getAbsolutePath());
			        } else {
			        }
			   
			}
		});
		chooseButton.setText("Choose");
		chooseButton.setBounds(289, 63, 84, 25);
		getContentPane().add(chooseButton);

		path = new JTextField();
		path.setEditable(false);
		path.setBounds(85, 63, 198, 25);
		getContentPane().add(path);

		final JLabel scegliIlPercorsoLabel = new JLabel();
		scegliIlPercorsoLabel.setText("Save selected result");
		scegliIlPercorsoLabel.setBounds(43, 22, 243, 25);
		getContentPane().add(scegliIlPercorsoLabel);
		
		
		//salva i risultati
		saveButton = new JButton();
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				progressBar.setIndeterminate(true);
				progressBar.setVisible(true);
				saveButton.setEnabled(false);

				
				String path=getPath();

				SalvaThread saveTh= new SalvaThread(selectedResult,path);
				saveTh.start();
				
			}
		});
		saveButton.setText("Save");
		saveButton.setBounds(85, 118, 111, 25);
		getContentPane().add(saveButton);

		final JLabel pathLabel = new JLabel();
		pathLabel.setText("Path:");
		pathLabel.setBounds(43, 68, 36, 15);
		getContentPane().add(pathLabel);


		
	}
	
	private String getPath(){
		return this.path.getText();
	}
	
	public void setRicerca(Ricerca ricerca){
		this.ricerca=ricerca;
	}
	

	
	//classe per lanciare il thread del salvataggio
	 class SalvaThread extends Thread {
		private List<Link> selectedResult;
		private String path;
		
         SalvaThread(List<Link> selectedResult, String path) {
             this.selectedResult = selectedResult;
             this.path=path;
             
         }
 
         public void run() {
             // compute primes larger than minPrime
        	 boolean success=Ricerca.saveResult(selectedResult,path);
        	 progressBar.setValue(100);
        	 progressBar.setIndeterminate(false);
        	 
        	 if (success){
					JOptionPane.showMessageDialog(null, "Save successfully !","Save result",JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}
				else{
					JOptionPane.showMessageDialog(null, "Error", "Save result", JOptionPane.ERROR_MESSAGE);
					dispose();
				}

         }

     }



}
